from django.http import HttpResponse
from django.conf import settings
import json
import logging
import datetime
import os
import sqlite3 

oozie_home='/export/datamarket/oozie/oozie-4.3.1/bin/'
logging.basicConfig(level=logging.DEBUG,format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',datefmt='%a, %d %b %Y %H:%M:%S')
def execute_Immediately(request):
	if request.method == 'POST':
		try:
			received_json_data=json.loads(request.body.decode().replace("'", "\""))
			logging.info("\n\n\n\t"+str(received_json_data)+"\n\n")
			jobid=received_json_data['jobid'].strip()
			db_path = os.path.normpath('/export/datamarket/OozieProperties/oozie_project/db.sqlite3') 
			db = sqlite3.connect(db_path) 
			# 通过cursor创建游标
			cursor = db.cursor()
			sql_jobid="select mission_val from oozie_configuration where jobid ={}".format(jobid)
			cursor.execute(sql_jobid)
			mission_val=cursor.fetchall()
			mission_val=mission_val[0][0]
			cursor.close()
			db.commit()
			db.close()
			today='2018-11-10'
			result={'result':mission_val+'现在执行','code':'0'}
			logging.info(result)
			os.chdir("/export/datamarket/OozieExec/"+today+"/"+mission_val+"/")
			os.system(oozie_home+"oozie job -config jobImmediately.properties -run")
			return HttpResponse(json.dumps(result,ensure_ascii=False),content_type="application/json;charset=utf-8")
	
		except:
			logging.info('\n\n\n\t输入格式有误\n\n')
			resultError={'result':'没有执行','code':'1'}
			return HttpResponse(json.dumps(resultError,ensure_ascii=False),content_type="application/json;charset=utf-8")
	else:
		pass

